package com.sizu.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.dto.Page;
import com.sizu.common.statisticaldto.TStatementDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TStatement;
import com.sizu.mapper.TStatementMapper;
import com.sizu.service.ITStatementService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-02-29
 */
@RestController
@RequestMapping("/statistical/tStatement")
public class TStatementController {

    @Resource
    private ITStatementService itStatementService;


    /**
     * 按月统计
     * @param tStatementDto
     * @return
     */
    @RequestMapping("/monthlist")
    public ResultEntity tStatementMonthList(TStatementDto tStatementDto){
        PageHelper.startPage(tStatementDto.getPageNum(),tStatementDto.getPageSize());
        TStatement tStatement=new TStatement();
        BeanUtils.copyProperties(tStatementDto,tStatement);
        List<TStatement> tStatements = itStatementService.getmonthsList(tStatement);
        PageInfo<TStatement> pageInfo=new PageInfo<>(tStatements);
        return ResultEntity.ok(pageInfo);
    }

    /**
     * 按日统计
     * @param tStatementDto
     * @return
     */
    @RequestMapping("/daylist")
    public ResultEntity tStatementDayList(TStatementDto tStatementDto){
        PageHelper.startPage(tStatementDto.getPageNum(),tStatementDto.getPageSize());
        TStatement tStatement=new TStatement();
        BeanUtils.copyProperties(tStatementDto,tStatement);
        List<TStatement> tStatements = itStatementService.getdayList(tStatement);
        PageInfo<TStatement> pageInfo=new PageInfo<>(tStatements);
        return ResultEntity.ok(pageInfo);
    }

    /**
     * 按日统计导出
     * @param tStatementDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/downdayexcel")
    public ResponseEntity tStatementDownDayExcel(TStatementDto tStatementDto) throws Exception {
        XSSFWorkbook xSSFWorkbooks = itStatementService.downLoadDayExcel(tStatementDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("按日统计盈利.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);
    }

    /**
     * 按月统计导出
     * @param tStatementDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/downmonthexcel")
    public ResponseEntity tStatementDownMonthExcel(TStatementDto tStatementDto) throws Exception {
        XSSFWorkbook xSSFWorkbooks = itStatementService.downLoadMonthExcel(tStatementDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("按月统计盈利.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);
    }

}
